<?php
namespace app\api\controller\v1;

use app\api\model\v1\Feedback as FeedbackModel;

/**
 * 意见反馈/故障报修
 * 访问：/api/v1.feedback/方法名；
 */
class Feedback extends Base
{
    // 查询最近的 10 条；
    public function index() {
        try {
            $params = $this->jmParameter(['account_id', 'type_id', 'page']);
            $validateResult = $this->validate($params,
                $this->jmSetRule([
                    'type_id|类型ID' => 'require|in:1,2',// 1表示意见反馈，2表示故障报修。
                    'page|页码' => ['require', 'regex'=>'/[1-9]\d*/'],
                ]),
                $this->jmSetMessage()
            );
            if (true !== $validateResult) {
                return $this->jmReturnJson(4001, $validateResult);
            }

            $this->jmTrans2int($params, ['account_id', 'type_id', 'page']);
            $pagesize = 5;

            // 最近10条
            // $models = FeedbackModel::field(['id', 'content', 'created_at', 'reply_content', 'reply_at'])
            $models = FeedbackModel::field(['content', 'created_at', 'IF(admin_id>0,"admin","user")'=>'who'])
                ->where([
                    'user_id' => $params['account_id'],
                    'type_id' => $params['type_id']
                ])
                // ->order('created_at DESC')
                ->order('id DESC')
                ->page($params['page'], $pagesize)
                ->select();

            $hasMore = true;
            if (count($models) < $pagesize) {
                $hasMore = false;
            }

            $models = array_reverse($models);

            // 用户头像，在登录成功后意见返回了，这里不必再次返回。
            
            return $this->jmReturnJson(200, '获取数据成功', [
                'list' => $models,
                'hasMore' => $hasMore
            ]);
        } catch (\Exception $e) {
            return $this->jmException($e);
        }
    }

    // 提交反馈的意见或报修的故障
    public function commit() {
        try {
            $params = $this->jmParameter(['account_id', 'content', 'type_id']);
            $validateResult = $this->validate($params,
                $this->jmSetRule([
                    'content|内容' => 'require',
                    'type_id|类型ID' => 'require|in:1,2'
                ]),
                $this->jmSetMessage()
            );
            if (true !== $validateResult) {
                return $this->jmReturnJson(4001, $validateResult);
            }

            $this->jmTrans2int($params, ['account_id', 'type_id']);

            $contentMd5 = md5($params['content']);

            // 重复性校验
            $contentMd5Array = FeedbackModel::where([
                'user_id' => $params['account_id'],
                'type_id' => $params['type_id']
            ])->column('content_md5');
            if (!empty($contentMd5Array) && in_array($contentMd5, $contentMd5Array)) {
                return $this->jmReturnJson(4002, '内容已存在');
            }

            $model = new FeedbackModel();
            $affectedRows = $model->save([
                'user_id' => $params['account_id'],
                'content' => $params['content'],
                'content_md5' => $contentMd5,
                'type_id' => $params['type_id']
            ]);

            if ($affectedRows) {
                return $this->jmReturnJson(200, '提交成功', [
                    'list' => [
                        [
                            'content' => $model->content,
                            'created_at' => $model->created_at,
                            'who' => 'user'
                        ]
                    ]
                ]);
            }
            
            return $this->jmReturnJson(4003, '提交失败');
        } catch (\Exception $e) {
            return $this->jmException($e);
        }
    }
}